﻿package birdry.ui
{
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	/**
	* ...
	* @author makeFont
	*/
	public class DragMap extends EventDispatcher	{
		
		var _container:Sprite;
		var _maskMc:Sprite;
		var _hit:SimpleButton;
		var _speed:Number;
		
		var _minX:Number;
		var _maxX:Number;
		var _minY:Number;
		var _maxY:Number;
		var _deltaX:Number;
		var _deltaY:Number;
		var _maxZoom:Number;
		var _accel:Number = 0;
		var _containerPoint:Point = new Point();
		var _containerSavePoint:Point = new Point();
		var _mouseSavePoint:Point = new Point();
		var _mouseDeltaPoint:Point = new Point();
		var _dragFlag:Boolean;
		
		public function zoom(ratio:Number): void	{
			_container.scaleX = 1 + (ratio*_maxZoom);
			_container.scaleY = 1 + (ratio * _maxZoom);
			setSpace();
			var tempPoint:Point = new Point(_container.x, _container.y);
			_container.x = returnLimitPoint(tempPoint).x;
			_container.y = returnLimitPoint(tempPoint).y;
		}
		
		private function returnLimitPoint(targetPoint:Point): Point	{
			if (targetPoint.x < _maxX)		targetPoint.x = _maxX;
			if (targetPoint.y < _maxY)		targetPoint.y = _maxY;
			if (targetPoint.x > _minX)		targetPoint.x = _minX;
			if (targetPoint.y > _minY)		targetPoint.y = _minY;
			return targetPoint;
		}
		
		public function DragMap(container:Sprite, hit:SimpleButton, maskMc:Sprite, speed:Number=0.2, maxZoom:Number=1)	{
			_container = container;
			_maskMc = maskMc;
			_hit = hit;
			_maxZoom = maxZoom;
			_speed = speed;
			setLayout();
			addEvent();
		}
		
		private function setLayout(): void	{
			setSpace();
		}
		
		private function setSpace():void 		{
			_minX = _maskMc.x;
			_maxX = _maskMc.x + -(_container.width - _maskMc.width);
			_minY = _maskMc.y;
			_maxY = _maskMc.y + -(_container.height - _maskMc.height);
			_deltaX = _maxX - _minX;
			_deltaY = _maxY - _minY;
		}
		
		private function addEvent(): void	{
			_hit.addEventListener( MouseEvent.MOUSE_DOWN, function(e:MouseEvent): void	{
				_dragFlag = true;
				_mouseDeltaPoint.x = 0;
				_mouseDeltaPoint.y = 0;
				_containerPoint.x = _container.x;
				_containerPoint.y = _container.y;
				_containerSavePoint.x = _container.x;
				_containerSavePoint.y = _container.y;
				_mouseSavePoint.x = _container.stage.mouseX;
				_mouseSavePoint.y = _container.stage.mouseY;
				_container.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
				_container.stage.addEventListener(Event.ENTER_FRAME, enterframeHandler);
				_container.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			});
		}
		
		private function mouseMoveHandler(e:MouseEvent):void 		{
			_mouseDeltaPoint.x = _mouseSavePoint.x - _container.stage.mouseX;
			_mouseDeltaPoint.y = _mouseSavePoint.y - _container.stage.mouseY;
		}
		
		private function enterframeHandler(e:Event):void 		{
			var targetPoint:Point = new Point( _containerSavePoint.x - _mouseDeltaPoint.x, _containerSavePoint.y - _mouseDeltaPoint.y);
			targetPoint = returnLimitPoint(targetPoint);
			_containerPoint.x += _speed * (targetPoint.x - _containerPoint.x);
			_containerPoint.y += _speed * (targetPoint.y - _containerPoint.y);
			_container.x = int(_containerPoint.x);
			_container.y = int(_containerPoint.y);
				
			if (Math.abs(_containerPoint.x - targetPoint.x) < 1 && Math.abs(_containerPoint.y - targetPoint.y) < 1 && !_dragFlag)	{
				_container.x = targetPoint.x;
				_container.y = targetPoint.y;
				_container.stage.removeEventListener(Event.ENTER_FRAME, enterframeHandler);
			}
		}
		
		
		private function mouseUpHandler(e:MouseEvent):void 		{
			_dragFlag = false;
			_container.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			_container.stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
		}
		
	}	// class
	
}	// package